package com.sangmo.fts.config;

import com.sangmo.boot.framework.common.session.UserSession;
import com.sangmo.fts.common.CoreFramework;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

public class CoreFrameworkFilter implements Filter {
    private static Logger LOG = LoggerFactory.getLogger(CoreFrameworkFilter.class);

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        try
        {
            CoreFramework.getInstance().start();
            UserSession.setLocale(request.getLocale());

            filterChain.doFilter(request, response);

            CoreFramework.getInstance().stop();
        }
        catch(Exception ex)
        {
            CoreFramework.getInstance().rollback();
            LOG.error("Error in Filter", ex);
        }
    }
}
